﻿Imports HMSLibrary
Imports Microsoft.ApplicationBlocks.Data
Imports System.Data
Imports HMSDataLayer
Imports System.Collections.ObjectModel

Public Class DiagnosisControl
    Implements IHMSData

    Public Property ID As Integer Implements HMSLibrary.IHMSData.ID
    Private Sub DiagnosisControl_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        ActionMode = FormAction.ADD
        Dim dataProvider As New MasterDataLayer()
        Dim lstCl As ObservableCollection(Of Diagnosis)
        lstCl = dataProvider.GetDiagnosisList()
        DiagnosisTreeView.ItemsSource = lstCl

    End Sub

    Public Property ActionMode As HMSLibrary.FormAction Implements HMSLibrary.IHMSData.ActionMode

    Public Function DataValidation() As Boolean Implements HMSLibrary.IHMSData.DataValidation
        DataValidation = False
        Try
            If String.IsNullOrEmpty(DiagnosisTextBox.Text) Then
                CommonMessages.ShowValidationMsg("Please enter procedure!")
                Exit Function
            End If

            If ActionMode = FormAction.ADD Then
                Dim strSql As String
                strSql = "SELECT  DiagnosisID FROM dbo.Diagnosis WHERE  ([Diagnosis] = N'" & DiagnosisTextBox.Text.Trim & "')"
                Dim objTemp As Object = Nothing
                objTemp = AppCommon.db.ExecuteScalar(CommandType.Text, strSql)
                If objTemp IsNot Nothing Then
                    If IsDBNull(objTemp) = False Then
                        CommonMessages.ShowValidationMsg("Diagnosis already exist!")
                        Exit Function
                    End If
                End If
            End If
            DataValidation = True
        Catch ex As Exception
            AppCommon.LogError(ex)
        End Try
    End Function

    Public Sub LoadData(ByVal ID As Integer) Implements HMSLibrary.IHMSData.LoadData
        ActionMode = FormAction.EDIT
    End Sub

    Public Function SaveData() As Boolean Implements HMSLibrary.IHMSData.SaveData
        If DataValidation() = False Then Return False
        Dim DataObject As New MasterDataLayer()
        Dim lup As New Diagnosis
        lup.Name = DiagnosisTextBox.Text.ToLower.Trim
        If DiagnosisTreeView.SelectedValue IsNot Nothing Then lup.ParentID = DirectCast(DiagnosisTreeView.SelectedValue, Diagnosis).ID
        lup.Description = RemarksTextBox.Text.Trim
        DataObject.SaveDiagnosis(lup, ActionMode)
        Return True
    End Function

    Public Sub AddNew() Implements HMSLibrary.IHMSData.AddNew
        ActionMode = FormAction.ADD
        DiagnosisTextBox.Text = String.Empty
        RemarksTextBox.Text = String.Empty
    End Sub

    Public Sub FindData() Implements HMSLibrary.IHMSData.FindData
        MainGrid.Visibility = Visibility.Collapsed
        FindStackPanel.Visibility = Visibility.Visible
        Dim DataObject As MasterDataLayer
        Dim DataCollection As Object
        DataObject = New MasterDataLayer()
        DataCollection = DataObject.GetTreatmentProceduresList
        SearchDataGrid.ItemsSource = DataCollection
    End Sub

    Public Sub BackView() Implements HMSLibrary.IHMSData.BackView
        MainGrid.Visibility = Visibility.Visible
        FindStackPanel.Visibility = Visibility.Collapsed
    End Sub

    Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    End Sub
End Class
